unit cuSimpleINI;

interface

type
  TINIDataSet = record
    Name : string;
    Value : string;
  end;

  TSimpleINI = class (TObject)
  private
    m_Path     : string;
    m_Count    : integer;
    m_Settings : array of TINIDataSet;
  public
    constructor Create();
    destructor Destroy(); override;
    procedure Reset();
    function Find(nName:string):string;
    function sFind(nName:string):string;
    function iFind(nName:string):integer;
    function rFind(nName:string):real;
    function bFind(nName:string):boolean;
    function Add(nName,nValue:string):string;
    function Load(nFileName:string):boolean;
    procedure Save();

    property Path : string read m_Path;
  end;

implementation

uses cuSplitFile, SysUtils, Dialogs, cuDebug, cuFuncs;

constructor TSimpleINI.Create();
begin
  inherited;
  Reset();
end;
destructor TSimpleINI.Destroy();
begin
  Reset();
  inherited;
end;

procedure TSimpleINI.Reset();
begin
  m_Count := 0;
  SetLength(m_Settings, m_Count);
end;

function TSimpleINI.Find(nName:string):string;
var I : integer;
begin
  result := '';

  for I := 0 to high(m_Settings) do
  begin
    if (m_Settings[I].Name = nName) then
    begin
      result := m_Settings[I].Value;
      DevConsole.Print(LOG_DEBUG3, 'Found match for "'+nName+'" -> '+result);
      exit;
    end;
  end;

  DevConsole.Print(LOG_CRITICAL, 'Error! Cannot find a match for "'+nName+'"');
end;

function TSimpleINI.sFind(nName:string):string;
begin
  result := Find(nName);
end;

function TSimpleINI.iFind(nName:string):integer;
begin
  try
    result := StrToInt( Find(nName) );
  except
    result := 0;
  end;
end;

function TSimpleINI.rFind(nName:string):real;
begin
  try
    result := StrToFloat( Find(nName) );
  except
    result := 0;
  end;
end;

function TSimpleINI.bFind(nName:string):boolean;
begin
  try
    result := StrToBool( Find(nName) );
  except
    result := FALSE;
  end;
end;

function TSimpleINI.Add(nName,nValue:string):string;
var I : integer;
begin
  result := '';
  Inc( m_Count );
  for I := 0 to m_Count-1 do
  begin
    if (m_Settings[I].Name = nName) then
      result := m_Settings[I].Value;
  end;
end;

function TSimpleINI.Load(nFileName:string):boolean;
var SplitFile : tSplitFile; Count : integer;
begin
  result := TRUE;
  try
    m_Path := GetEXEPath() + nFileName;

    if (not FileExists(m_Path)) then
    begin
      DevConsole.Print( LOG_CRITICAL, 'Error! Cannot find the Settings File. -> '+m_Path);
      result := FALSE;
      m_Path := '';
      exit;
    end;

    SplitFile := TSplitFile.Create;
    SplitFile.SetChar('=','^','"');

    Reset();
    Count := 0;

    if ( SplitFile.LoadFile( m_path ) ) then
    begin
      while SplitFile.GetNextLine() do
      begin
        Inc(Count);
        SetLength(m_Settings, Count);

        m_Settings[Count-1].Name  :=  SplitFile.toStr(0);
        m_Settings[Count-1].Value :=  SplitFile.toStr(1);
        DevConsole.Print(LOG_DEBUG1, 'settings: "'+m_Settings[Count-1].Name+'" = '+m_Settings[Count-1].Value);
      end;
    end;
  finally
    FreeAndNil( SplitFile );
  end;
end;

procedure TSimpleINI.Save(); // to do
var SplitFile : tSplitFile; Count : integer;
begin
  SplitFile := TSplitFile.Create;
  SplitFile.SetChar('=','^','"');

  Count := 0;

  if ( SplitFile.LoadFile( m_path ) ) then
  begin
    while SplitFile.GetNextLine() do
    begin
      Inc(Count);
      SetLength(m_Settings, Count);

      m_Settings[Count-1].Name  :=  SplitFile.toStr(0);
      m_Settings[Count-1].Value :=  SplitFile.toStr(1);
    end;
  end;
end;

end.
